<?php
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
require('D:/Projects/gagcrawler/includes/functions.php');
require('D:/Projects/gagcrawler/includes/simple_html_dom.php');
$url = 'http://epic.vn/?page=';


function getEpicPost($url){
	// create a new cURL resource
	$ch = curl_init();

	$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
	$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
	$header[] = "Cache-Control: max-age=0";
	$header[] = "Connection: keep-alive";
	$header[] = "Keep-Alive: 300";
	$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
	$header[] = "Accept-Language: en-us,en;q=0.5";
	$header[] = "Pragma: "; // browsers keep this blank.
	// set URL and other appropriate options
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	//curl_setopt($ch, CURLOPT_PROXY, '210.245.0.24:80');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	// grab URL and pass it to the browser
	$responseContent = curl_exec($ch);
	// close cURL resource, and free up system resources
	curl_close($ch);

	$html = str_get_html($responseContent);
	$type = 'photo';
	$postTitle = '';
	$postImage = '';
	$videoCode = '';
	if(trim($html) != ''){
		foreach($html->find('div.badge-entry-collection article') as $element){
			$postContain = $element->find('div.post-container', 0);
			$vid = $postContain->find('div.badge-video-container', 0);
			
			if(trim($postContain->innertext) != ''){
				$img = $postContain->find('img', 0);
				$title = $img->alt;
				$src = $img->src;
				$link = $postContain->find('a', 0)->href;
				$type = 'photo';
				if($vid){
					$type = 'video';
					if(preg_match('/.*data-video-id="(.*?)".*/is', $vid->outertext, $match)){
						$videoCode = $match[1];
						$src = 'http://img.youtube.com/vi/'.$videoCode.'/0.jpg';
					}
					else{
						continue;
					}
				}
				if(!$title || !$src || !$link) continue;
				$unique = md5($src);
				echo "\tcrawling [".end(explode('/',$link))."] .. ";
				$postTitle = mysql_real_escape_string(strip_tags($title));
				$postUrl = mysql_real_escape_string($link);
				//$postImage = mysql_real_escape_string($postImage);
				
				$fileInfo = imageDownloader($src);
				if(!$fileInfo) continue;
				$postImage = $fileInfo['name'];
				if($type == 'photo'){
					if(isAnimatedImage(IMAGE_PATH.$postImage)){
						$type == 'gif';
					}
				}
				$parse = parse_url($link);
				$sqlInsert = "INSERT INTO data(original_url, post_title, post_image, post_unique, crawl_date, file_checksum,item_type,video_code,source_page) VALUES('".$postUrl."', '$postTitle', '$postImage', '$unique', NOW(), '".$fileInfo['checksum']."', '$type', '$videoCode', '".$parse['host']."')";

				if(mysql_query($sqlInsert)){
					echo "\tdone\n";
				}
				else{
					echo "\tfailed -> \n".mysql_error()."\n\n";
					#die("-------BREAK-------");
				}
				
			}
		}
	}
}
require('D:/Projects/gagcrawler/dbconnect.php');
#echo '<ol>';
$numPage = 20;
for($i = 1; $i <=$numPage; $i++){
	#echo '<li>';
	echo "----------------------\nCrawling page $i/$numPage:\n";
	getEpicPost($url.$i);
	sleep(5);
	#echo '</li>';
}
echo "\n----------------------[DONE]----------------------\n";
#echo '<ol>';
